package Server.init;

import Server.dao.DictionarysDao;
import Server.dao.NavigationDao;
import Server.dao.PermissionsDao;
import Server.dao.RoleDao;
import Server.entity.*;
import Server.service.RedisService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Configuration
public class BootConfiguration {
    @Bean
    public String RedisLoad(RedisService redisService, DictionarysDao dictionaryDao, NavigationDao navigationDao, RoleDao roleDao, PermissionsDao permissionsDao){
        //将字典中的表头加载进redis
        /*List<Diction> dictionaries = dictionaryDao.findByType(DataResources.Type.TABLE.name());
        for(Diction dic:dictionaries){
            redisService.set(dic.getKeys(),dic.getVal());
        }
        //将导航按角色加载进redis
        List<Role> roles = roleDao.findAll();
        for (Role role:roles){
            List<Map<Object, Object>> rqNav = new LinkedList<Map<Object, Object>>();
            List<Permissions> permissions = permissionsDao.findByRole(role);
            List<Navigation> navigations = navigationDao.findByPermissionsAndStatus(permissions,Navigation.Status.YES);
            for(Navigation fp :navigations){
                if (fp.getNavigation()==null) {
                    JSONObject par = JSONObject.fromObject(fp);
                    Map<Object, Object> pars = (Map) par;
                    List<Map<Object, Object>> sonNav = new LinkedList<Map<Object, Object>>();
                    for (Navigation son : navigations) {
                        if (son.getNavigation()!=null) {
                            if (son.getNavigation().equals(fp)) {
                                JSONObject sonJson = JSONObject.fromObject(son);
                                Map<Object, Object> sonMp = (Map) sonJson;
                                sonMp.put("id", son.getUuid());
                                sonMp.put("name", son.getName());
                                sonMp.put("icon", son.getIcon());
                                sonMp.put("url", "./vsp_" + son.getUri());
                                sonNav.add(sonMp);
                            }
                        }
                    }
                    pars.put("id", fp.getUuid());
                    pars.put("name", fp.getName());
                    pars.put("icon", fp.getIcon());
                    pars.put("childList", sonNav);
                    rqNav.add(pars);
                }
            }
            redisService.set(role.getUuid(), JSONArray.fromObject(rqNav).toString());
        }*/
        return null;
    }
}
